<?php
header('Content-Type:text/html;charset=utf-8');
/**
 * 
 * 用户信息注册表
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-2-25
 * model() 创建模型对象，静态
 * tableName() 返回表名
*/

class User extends CActiveRecord{
	//确认密码验证
	public $user_password_confirm;
	public $code;
	/**
	 *返回当前模型对象的方法 
	 */
	public static function model($className = __CLASS__){
		return parent::model($className);
	}
	/**
	 * 返回当前表名
	 */
	public function tableName(){
		return '{{user}}';
	}
	function attributeLabels(){
		return array(
			'user_name' => '用 户 名',
			'user_password' => '密　　码',
			'user_pass_confirm' => '确认密码',
			'user_email' => '电子邮箱',
			'user_tel' => '手　　机',
			'user_phone' => '固定电话',
			'user_question' => '验证问题',
			'user_answer' => '验证回答',
			'role_id' => '身份选择',
			'sel_name' => '营业执照名',
			'sel_number' => '执照编号',
			'user_birth' => '出生日期',
			'user_addr' => '家庭地址',
			'user_job' => '工　　作',
			'user_hobby' => '爱　　好',
			'user_it' => '身 份 证',
			'user_sex' => '性　　别',
			'code' => '验 证 码',
		);
	}
	public function rules(){
		return array(
			array('code','length','is'=>4,'message'=>"( *验证码必须是四位)"),
			array('user_name','required','message'=>"( *用户名必须填写!)"),
			array('user_name','unique','message'=>"( *用户名已存在!)"),
			array('user_name','check_length'),
			array('user_password','required','message'=>"( *密码必须填写!)"),
			array('user_password_confirm','compare','compareAttribute'=>'user_password','message'=>"( *两次密码必须一致!)"),
			array('user_email','email','allowEmpty'=>false,'message'=>"( *请输入正确的邮箱格式！)"),
			array('user_tel','required','message'=>"( *手机号不得为空)"),
			array('user_tel','match','pattern'=>'/^1[3,5,7,8]{1}\d{9}$/','message'=>"( 请输入正确的手机号码！)"),
			array('user_it','match','pattern'=>'/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/','message'=>"( 请输入正确的身份证号码！)"),
			array('user_phone','match','pattern'=>'/^\d{4}-\d{8}$/','message'=>"( 请输入正确的固定电话号码 区号+号码！)"),
			array('user_question','in','range'=>array(2,3,4,5),'message'=>"( *请选择问题)"),
			array('user_answer','required','message'=>"( *验证回答必须填写！千万不可忘记)"),
			
			array('user_sex','required','message'=>"( *性别必须填写！)"),
//			array('code','captcha','message'=>'请输入正确的验证码'),
			/**
			 * 自定义验证方法
			 */
			array('user_hobby','check_count'),
			//没有设置验证规则的属性使用safe，否则不给接收信息！！！
			array('user_birth,user_addr,user_job,user_integral,user_register,user_last_ip,user_last_time,role_id','safe'),
		);
	}
	
	/**
	 * 修改密码
	 */
	function updPassword($user_id){
		$cmodel = new CCCModel();
		$_sql = "UPDATE {{user}} set user_password='".sha1($_POST['n_password'])."' WHERE user_id=$user_id";
		$result = $cmodel->upd($_sql);
		return $result;
	}
	
	/**
	 * 根据用户id获取email
	 */
	function getEmailByUserId($user_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT user_email FROM {{user}} WHERE user_id=$user_id";
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	
	/**
	 * 修改密码核对
	 */
	function checkPassword($user_id,$r_password){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{user}} WHERE user_password='".$r_password."' AND user_id=$user_id";
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	/**
	 * 获得所有商家信息
	 */
	function getAllSal(){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{user}} WHERE active=1 AND {{user}}.role_id IN(
							SELECT {{role}}.role_id FROM {{role}} WHERE role_name='商家' AND active=1)";
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	/**
	 * 通过名字获取id
	 */
	function getIdByName($user_name){
		$cmodel = new CCCModel();
		$_sql = "SELECT user_id FROM {{user}} WHERE user_name=".$user_name;
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	/**
	 * 验证checkbox选择个数
	 */
	function check_count(){
		$len = strlen($this->user_hobby);
		if($len <= 0)
			$this->addError('user_hobby',"(* 至少选择一项)");//与mession一致
	}
	/**
	 * 验证输入长度
	 */
	function check_length(){
		$userNameLen = strlen($this->user_name);
		if($userNameLen > 20 || $userNameLen < 2){
			$this->addError('user_name','(* 必须在2到20位之间)');
		}
	}
	//操作成功的提示
	function _success($_string,$_url){
		echo "<script type='text/javascript'>alert('".$_string."');location.href='".$_url."';</script>";
	}
	//操作失败的提示
	function _error($_string){
		echo "<script type='text/javascript'>alert('".$_string."');history.back()</script>";
	}
}
?>